package impl.search;

import anaydis.search.SymbolTable;
import anaydis.search.text.TextIndex;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tomi
 * Date: 09/11/2011
 * Time: 15:20:37
 * To change this template use File | Settings | File Templates.
 */
public class TextIndexGenerator implements TextIndex {
    private SymbolTable<String, List<Integer>> T;
    private int n;
    public TextIndexGenerator(SymbolTable<String, List<Integer>> symbolTable) {
        T = symbolTable;
    }
    public void setN(int n) {
        this.n = n;
    }
    public void buildIndex(String s) {
        try{
            FileReader fr = new FileReader(s);
            int indice = 0;
            while(indice<n){
                String string = new String();
                int a = fr.read();
                while((65<=a && a<=90)||(97<=a && a<=122)){
                    string = string.concat(new Character((char)a).toString());
                    a = fr.read();
                }
                if(!string.equals("") && !T.containsKey(string)){
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(indice);
                    T.put(string, list);
                    indice++;
                }else if(!string.equals("")){
                    List<Integer> l = T.get(string);
                    l.add(indice);
                    T.put(string, l);
                    indice++;
                }
            }
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
    }
    public List<Integer> search(String s) {
        if(T.containsKey(s)){
            return T.get(s);
        }
        return null;
    }
}
